package com.jian.dao.impl;

import com.jian.dao.BaseBDao;
import com.jian.dao.StudentDAO;
import com.jian.pojo.Teacher;
import com.jian.util.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Jiange
 * @create 2021-04-12-15:40
 */
public class Student extends BaseBDao implements StudentDAO {
    @Override
    public int queryLogin(com.jian.pojo.Student student) {
        com.jian.pojo.Student one = null;
        String sql = "select * from student where username = ? and password = ? and account = ?";
        try {
            one = queryForOne(com.jian.pojo.Student.class,"select * from student where username = ?",student.getUsername());
            if(one == null){
                return -1;//用户名不存在
            }else{
                one = queryForOne(com.jian.pojo.Student.class,"select * from student where username = ? and password = ?",student.getUsername(),student.getPassword());
                if(one == null){
                    return -2;//密码错误
                }else{
                    one = queryForOne(com.jian.pojo.Student.class, sql, student.getUsername(), student.getPassword(), student.getAccount());
                    if (one == null){
                        return -3;//账号错误
                    }else return 1;//成功
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public int register(com.jian.pojo.Student student) {
        String sql = "select * from student where account=?";
        com.jian.pojo.Student student1 = null;
        try {
            student1 = queryForOne(com.jian.pojo.Student.class, sql, student.getAccount());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException();
        }
        if (student1 == null) {
            try {
                student1 = queryForOne(com.jian.pojo.Student.class, "select * from student where username=?", student.getUsername());
                System.out.println(student.getUsername());
                System.out.println(student1);
                if (student1 == null) {
                    try {
                        Teacher teacher = queryForOne(Teacher.class, "select * from teacher where account=?", student.getTeacher());
                        if (teacher != null) {
                            int i =update("insert into student(username,account,password,teacher)values(?,?,?,?)",
                                    student.getUsername(), student.getAccount(), student.getPassword(), student.getTeacher()
                            );
                            System.out.println(i);
                            return 1;//成功
                        } else return -2;//老师不存在
                    } catch (Exception throwables) {
                        throwables.printStackTrace();
                        throw new RuntimeException();
                    }
                } else {
                    return -3;//用户名存在
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                System.out.println("register出现错误");
                throw new RuntimeException();
            }
        }
        return -1;//表示学生账号存在
    }


    @Override
    public int addStudent(com.jian.pojo.Student student) {
        String sql = "insert into student(username,account,password,teacher) values(?,?,?,?)";
        int update;
        try {
            update = update(sql, student.getUsername(), student.getAccount(), student.getPassword(), student.getTeacher());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException();
        }
        return update;
    }
}
